package gold.enterprise;

public class TencentPCGOne {

    public static boolean isMatch(String str1, String str2) {
        if (null == str1) return null == str2;
        if (null == str2) return null == str1;

        if (str1.length() == str2.length()) {
            //同长度只能通过改来判断
            int i = 0, j = 0;
            boolean flag = false;   //是否已经失配过一次的标记位
            while (i <= str1.length() - 1) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    ++i;
                    ++j;
                    continue;
                }

                if (flag) {
                    // 已经失配过一次，没有第二次机会了
                    return false;
                } else {
                    ++i;
                    ++j;
                    flag = true;
                }
            }

            return true;
        }

        if (str1.length() == str2.length() - 1) {
            //str1 短，需要str1增的方式判断
            int i = 0, j = 0;
            int misMatchPos = -1;   //失配位置
            while (i <= str1.length() - 1 && j <= str2.length() - 1) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    ++i;
                    ++j;
                    continue;
                }

                if (misMatchPos >= 0) return false; //说明之前已经失配过了
                ++j;    //j 需要跳过
                misMatchPos = i;//i 位置 str失配，可以通过增
            }
            return true;
        }

        if (str1.length() - 1 == str2.length()) return isMatch(str2, str1);//函数自递归

        //这些情况都未命中返回false
        return false;
    }


    public static void main(String[] args) {
        //Scanner in = new Scanner(System.in);
        //int a = in.nextInt();
        //System.out.println(a);
        // System.out.println("Hello World!");

        //增
        System.out.println(isMatch("snapchat", "snapchat5"));//增
        System.out.println(isMatch("snapchat", "5snapchat"));//增
        System.out.println(isMatch("snapchat", "snap5chat"));//增

        //改
        System.out.println(isMatch("snapchat", "snabchat"));//改
        System.out.println(isMatch("snapchat", "5napchat"));//改
        System.out.println(isMatch("snapchat", "snapcha2"));//改

        //删
        System.out.println(isMatch("snapchat", "snachat"));//删
        System.out.println(isMatch("snapchat", "napchat"));//删
        System.out.println(isMatch("snapchat", "snapcha"));//删

        // 不能改变
        System.out.println(isMatch("snapchat", "snap11chat"));//不能改变，返回false
        System.out.println(isMatch("snapchat", "snapcha45"));//不能改变，返回false
        System.out.println(isMatch("snapchat", "5napchat8"));//不能改变，返回false
        System.out.println(isMatch("snapchat", "11napchat"));//不能改变，返回false
    }

}


